<?php

class BasketbalHandlerFunctions
{

public function AddPlayerToOpponentTeam($season, $teamname, $playerID, $playernumber, $playertype)
{

	require __DIR__ . '/db_bb_connect.php';

	$sql = "SELECT teamID FROM teams_list WHERE teamname = '$teamname'";

	$result=$db->query($sql);
	$row=$result->fetch_assoc();

	$teamID = $row['teamID'];

	$sql = "INSERT INTO teams_opponents (playerID, teamID, season, playernumber, teamname, playertype) VALUES ('$playerID', '$teamID','$season', $playernumber, '$teamname', '$playertype')";

	try{
		$dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
		$dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$stmt = $dbpdo->prepare($sql);
		$stmt->bindValue(":null", 0, PDO::PARAM_INT);
		if (!$stmt->execute()) {
		echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		} 

		$sql = "SELECT firstname, lastname
		FROM users
		WHERE user_key = '$playerID'";

		$result=$db->query($sql);
		$row=$result->fetch_assoc();
		$firstname = $row['firstname'];
		$lastname = $row['lastname'];
		echo "<div><center>$firstname $lastname succesfully added to $teamname</center></div>";
  		
    } catch (PDOException $e) {    
      	$error = $e->getMessage();
      	echo $error;
    }
}

public function AddPlayerToCoachTeam($season, $teamname, $playerID, $playernumber, $playertype)
{

	require __DIR__ . '/db_bb_connect.php';

	$sql = "SELECT teamID FROM teams_list WHERE teamname = '$teamname'";

	$result=$db->query($sql);
	$row=$result->fetch_assoc();

	$teamID = $row['teamID'];

	$sql = "INSERT INTO teams (playerID, teamID, season, playernumber, teamname, playertype) VALUES ('$playerID', '$teamID','$season', $playernumber, '$teamname', '$playertype')";

	try{
		$dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
		$dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$stmt = $dbpdo->prepare($sql);
		$stmt->bindValue(":null", 0, PDO::PARAM_INT);
		if (!$stmt->execute()) {
		echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		} 

		$sql = "SELECT firstname, lastname
		FROM users
		WHERE user_key = '$playerID'";

		$result=$db->query($sql);
		$row=$result->fetch_assoc();
		$firstname = $row['firstname'];
		$lastname = $row['lastname'];
		echo "<div><center>$firstname $lastname succesfully added to $teamname</center></div>";
  		
    } catch (PDOException $e) {    
      	$error = $e->getMessage();
      	echo $error;
    }
}

public function UpdatePlayerQuarterStats($season, $gamenumber, $quarter, $playerID, $DRB, $ORB, $ST, $TOV, $AST, $BS, $FGIscored, $FGItotal, $FGOscored, $FGOtotal, $THPscored, $THPtotal, $FTscored, $FTtotal, $PF, $MP)
{
	require __DIR__ . '/db_bb_connect.php';

	$sql =  "UPDATE stats_game_quarter
      SET ORB = $ORB, DRB = $DRB, ST = $ST, TOV = $TOV, PF = $PF, FGIscored = $FGIscored, FGItotal = $FGItotal, FTscored = $FTscored, FTtotal = $FTtotal, FGOscored = $FGOscored, FGOtotal = $FGOtotal, THPscored = $THPscored, THPtotal = $THPtotal, MP = $MP, AST = $AST, BS = $BS
      WHERE playerID = '$playerID' 
      AND gamenumber = '$gamenumber' 
      AND season = '$season'
      AND quarter = '$quarter'";

	try{
		$dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
		$dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$stmt = $dbpdo->prepare($sql);
		$stmt->bindValue(":null", 0, PDO::PARAM_INT);
		if (!$stmt->execute()) {
		echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		} 

		$sql = "SELECT firstname, lastname
		FROM users
		WHERE user_key = '$playerID'";

		$result=$db->query($sql);
		$row=$result->fetch_assoc();
		$firstname = $row['firstname'];
		$lastname = $row['lastname'];
		echo "<div><center>Stats of $firstname $lastname succesfully updated</center></div>";
  		
    } catch (PDOException $e) {    
      	$error = $e->getMessage();
      	echo $error;
    }

}



}